package ynu.edu.ky_elm_springboot3.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import ynu.edu.ky_elm_springboot3.po.Cart;
import ynu.edu.ky_elm_springboot3.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Tag(name = "购物车相关接口")
@RestController
@RequestMapping("/CartController")
public class CartController {

    @Autowired
    private CartService cartService;
    
    @Operation(summary = "查询购物车")
    @GetMapping("/listCart")
    public List<Cart> listCart(@RequestParam("userId") String userId,
                              @RequestParam(value = "businessId", required = false) Integer businessId) {
        return cartService.listCart(userId, businessId);
    }
    
    @Operation(summary = "添加购物车")
    @PostMapping("/saveCart")
    public int saveCart(@RequestParam("userId") String userId,
                       @RequestParam("businessId") Integer businessId,
                       @RequestParam("foodId") Integer foodId) {
        Cart cart = new Cart();
        cart.setFoodId(foodId);
        cart.setBusinessId(businessId);
        cart.setUserId(userId);
        return cartService.saveCart(cart);
    }
    
    @Operation(summary = "更新购物车")
    @PutMapping("/updateCart")
    public int updateCart(@RequestParam("userId") String userId,
                         @RequestParam("businessId") Integer businessId,
                         @RequestParam("foodId") Integer foodId,
                         @RequestParam("quantity") Integer quantity) {
        Cart cart = new Cart();
        cart.setUserId(userId);
        cart.setBusinessId(businessId);
        cart.setFoodId(foodId);
        cart.setQuantity(quantity);
        return cartService.updateCart(cart);
    }
    
    @Operation(summary = "移除购物车")
    @DeleteMapping("/removeCart")
    public int removeCart(@RequestParam("userId") String userId,
                         @RequestParam("businessId") Integer businessId,
                         @RequestParam(value = "foodId", required = false) Integer foodId) {
        return cartService.removeCart(userId, businessId, foodId);
    }
} 